package com.muyu.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.system.domain.DmKeys;
import com.muyu.system.mapper.DmKeysMapper;
import com.muyu.system.service.IDmKeysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 令牌信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-16
 */
@Service
public class DmKeysServiceImpl implements IDmKeysService {
    @Autowired
    private DmKeysMapper dmKeysMapper;

    /**
     * 查询令牌信息
     *
     * @param id 令牌信息主键
     * @return 令牌信息
     */
    @Override
    public DmKeys selectDmKeysById(Long id) {
        return dmKeysMapper.selectDmKeysById(id);
    }

    /**
     * 查询令牌信息列表
     *
     * @param dmKeys 令牌信息
     * @return 令牌信息
     */
    @Override
    public List<DmKeys> selectDmKeysList(DmKeys dmKeys) {
        return dmKeysMapper.selectDmKeysList(dmKeys);
    }

    /**
     * 新增令牌信息
     *
     * @param dmKeys 令牌信息
     * @return 结果
     */
    @Override
    public int insertDmKeys(DmKeys dmKeys) {
        dmKeys.setCreateTime(DateUtils.getNowDate());
        int flag = 0;
        for (int i = 0; i < 3; i++) {
            dmKeys.setCode(RandomUtil.randomString(6));
            flag = dmKeysMapper.insertDmKeys(dmKeys);
        }
        return flag;
    }

    /**
     * 修改令牌信息
     *
     * @param dmKeys 令牌信息
     * @return 结果
     */
    @Override
    public int updateDmKeys(DmKeys dmKeys) {
        int flag = 0;
        dmKeys.setUpdateTime(DateUtils.getNowDate());
        DmKeys dmKeysQuery = new DmKeys();
        dmKeysQuery.setCode(dmKeys.getCode());
        dmKeysQuery.setState(0L);
        List<DmKeys> dmKeysList = dmKeysMapper.selectDmKeysList(dmKeysQuery);
        if (dmKeysList.size() > 0) {
            dmKeys.setId(dmKeysList.get(0).getId());
            flag = dmKeysMapper.updateDmKeys(dmKeys);
            return flag;
        }
        return flag;
    }

    /**
     * 批量删除令牌信息
     *
     * @param ids 需要删除的令牌信息主键
     * @return 结果
     */
    @Override
    public int deleteDmKeysByIds(Long[] ids) {
        return dmKeysMapper.deleteDmKeysByIds(ids);
    }

    /**
     * 删除令牌信息信息
     *
     * @param id 令牌信息主键
     * @return 结果
     */
    @Override
    public int deleteDmKeysById(Long id) {
        return dmKeysMapper.deleteDmKeysById(id);
    }
}
